//
//  GatherDetailTableViewCell.m
//  UGoods
//
//  Created by dllo on 16/1/25.
//  Copyright © 2016年 蓝鸥科技.李贺. All rights reserved.
//

#import "GatherDetailTableViewCell.h"
#import "GatherModel.h"
#import "BaseView.h"
#import "BaseLabel.h"
#import <UIImageView+WebCache.h>
#import "ConstantsDefine.h"
@interface GatherDetailTableViewCell ()

@property (nonatomic, retain) BaseView *backView; /**< 背景 */
@property (nonatomic, retain) BaseLabel *titleLabel; /**< 标题 */
@property (nonatomic, retain) UIView *lineView;
@property (nonatomic, retain) BaseLabel *buyerBriefLabel; /**< 买手简介 */
@property (nonatomic, retain) UIView *goodsView; /**< 商品 */
@property (nonatomic, retain) UIImageView *goodsImageView; /**< 商品图片 */
@property (nonatomic, retain) UILabel *goodsNameLabel; /**< 商品名 */
@property (nonatomic, retain) UILabel *priceLabel; /**< 价格 */
@property (nonatomic, retain) UIButton *abutton;
@property (nonatomic, retain) BaseLabel *timeLabel;

@end

@implementation GatherDetailTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backView = [[BaseView alloc] init];
        [self.contentView addSubview:_backView];
        [_backView release];
        
        self.titleLabel = [[BaseLabel alloc] init];
        _titleLabel.text = @"对商品给予评价";
        _titleLabel.font = [UIFont systemFontOfSize:13 * WIDTH];
        [self.backView addSubview:_titleLabel];
        [_titleLabel release];
        
        self.lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor grayColor];
        [self.backView addSubview:_lineView];
        [_lineView release];
        
        // 买手评价
        self.buyerBriefLabel = [[BaseLabel alloc] init];
        //        _buyerBriefLabel.backgroundColor = [UIColor redColor];
        _buyerBriefLabel.font = [UIFont systemFontOfSize:12 * WIDTH];
        _buyerBriefLabel.numberOfLines = 0;
        _buyerBriefLabel.textColor = [UIColor grayColor];
        [self.backView addSubview:_buyerBriefLabel];
        [_buyerBriefLabel release];
        
        self.goodsView = [[UIView alloc] init];
        _goodsView.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
        [self.backView addSubview:_goodsView];
        [_goodsView release];
        
        self.goodsImageView = [[UIImageView alloc] init];
        [self.goodsView addSubview:_goodsImageView];
        [_goodsImageView release];
        
        self.goodsNameLabel = [[UILabel alloc] init];
        _goodsNameLabel.font = [UIFont systemFontOfSize:13 * WIDTH];
        _goodsNameLabel.numberOfLines = 0;
        [self.goodsView addSubview:_goodsNameLabel];
        [_goodsNameLabel release];
        
        self.priceLabel = [[UILabel alloc] init];
        _priceLabel.font = [UIFont systemFontOfSize:12 * WIDTH];
        _priceLabel.textColor = [UIColor redColor];
        [self.goodsView addSubview:_priceLabel];
        [_priceLabel release];
        
        self.abutton = [[UIButton alloc] init];
        _abutton.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_abutton];
        [_abutton addTarget:self action:@selector(handleTouch:) forControlEvents:UIControlEventTouchUpInside];
        
        self.timeLabel = [[BaseLabel alloc] init];
        _timeLabel.font = [UIFont systemFontOfSize:12 * WIDTH];
        [self.backView addSubview:_timeLabel];
        [_timeLabel release];
        
    }
    return self;
}

- (void)handleTouch:(UIButton *)button {
    NSLog(@"BBB");
    /* 协议传值 */
    if ([self.delegate respondsToSelector:@selector(pushBuyDetailWithStr:)]) {
        [self.delegate pushBuyDetailWithStr:[_model.good[@"id"] stringValue]];
    }
}

// 计算buyerBriefLabel宽度
+ (CGFloat)heightWithText:(NSString *)text {
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake(375 - 20 * WIDTH, 1000 * HEIGHT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:12 * WIDTH]} context:nil];
    
    return rect.size.height;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _backView.frame = CGRectMake(0, 5 * HEIGHT, self.contentView.frame.size.width, self.contentView.frame.size.height - 10 * HEIGHT);
    _titleLabel.frame = CGRectMake(10 * WIDTH, 10 * HEIGHT, 200 * WIDTH, 20 * HEIGHT);
    _lineView.frame = CGRectMake(10 * WIDTH, 40 * HEIGHT, self.contentView.frame.size.width - 20 * WIDTH, 1);
    CGFloat h = [[self class] heightWithText:_buyerBriefLabel.text];
    _buyerBriefLabel.frame = CGRectMake(10 * WIDTH, 55 * HEIGHT, self.contentView.frame.size.width - 20 * WIDTH, h);
    _goodsView.frame = CGRectMake(10 * WIDTH, self.contentView.frame.size.height - 120 * HEIGHT, self.contentView.frame.size.width - 20 * WIDTH, 80 * HEIGHT);
    _abutton.frame = CGRectMake(10 * WIDTH, self.contentView.frame.size.height - 120 * HEIGHT, self.contentView.frame.size.width - 20 * WIDTH, 80 * HEIGHT);
    _goodsImageView.frame = CGRectMake(5 * WIDTH, 5 * HEIGHT, 70 * WIDTH, 70 * HEIGHT);
    _goodsNameLabel.frame = CGRectMake(80 * WIDTH, 5 * HEIGHT, self.goodsView.frame.size.width - 90 * WIDTH, 40 * HEIGHT);
    _priceLabel.frame = CGRectMake(80 * WIDTH, 60 * HEIGHT, 200 * WIDTH, 15 * HEIGHT);
    _timeLabel.frame = CGRectMake(10 * WIDTH, self.contentView.frame.size.height - 30 * HEIGHT, 200 * WIDTH, 15 * HEIGHT);
}

- (void)setModel:(GatherModel *)model {
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    
    _buyerBriefLabel.text = model.pingjia;
    [_goodsImageView sd_setImageWithURL:[NSURL URLWithString:model.good[@"img_url"]] placeholderImage:[UIImage imageNamed:@"place2"]];
    _goodsNameLabel.text = model.good[@"goods_name"];
    _priceLabel.text = [NSString stringWithFormat:@"¥%@", model.good[@"shop_price"]];
    
    NSString *str = [model.created_at stringValue];
    NSTimeInterval time=[str doubleValue]+28800;//因为时差问题要加8小时 == 28800 sec
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    //    NSLog(@"date:%@",[detaildate description]);
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyy.MM.dd"];
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    _timeLabel.text = currentDateStr;
    
}


@end
